package com.fruit.controller.app.account;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.BaseDTO;
import com.fruit.pojo.user.UserReal;
import com.fruit.sdk.account.AccountManager;
import com.fruit.sdk.account.NoticeManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author joe
 * @version 2017.12.12 10:06
 */
@Slf4j
@Controller
@RequestMapping(value = "/app/account")
public class AccountAppController extends BaseController {
    @Autowired
    private AccountManager accountManager;
    @Autowired
    private NoticeManager noticeManager;


    /**
     * 获取指定用户的钱包
     *
     * @param uid 用户id
     * @return 用户钱包，包含佣金和余额
     */
    @RequestMapping(value = "getUserAccount", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getUserAccount(@RequestParam("userId") int uid) {
        log.info("获取用户{}的钱包", uid);

        try {
            AccountManager.Account account = accountManager.getUserAccount(uid);
            log.debug("用户{}的钱包为：{}", uid, account);
            return BaseDTO.buildSuccess(account).convert();
        } catch (Exception e) {
            log.error("获取用户{}的钱包出错", uid, e);
            return BaseDTO.buildError("获取用户钱包出错").convert();
        }
    }


    /**
     * 查询用户实名认证状态
     *
     * @param uid 用户ID
     * @return 返回1表示未申请认证，返回2表示申请认证未审核，返回3表示实名认证审核未通过，返回4表示实名认证审核已通过，
     * 返回5表示未知状态
     */
    @RequestMapping(value = "isReal", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> isReal(@RequestParam("userId") long uid) {
        log.info("获取用户{}实名认证状态", uid);

        try {
            UserReal userReal = accountManager.isReal(uid);

            log.debug("用户{}的实名认证状态为：{}", uid, userReal);
            int status = accountManager.checkUserReal(userReal);
            log.debug("用户{}的实名认证状态转换为：{}", uid, status);
            Map<String, Object> map = new HashMap<>();
            BaseDTO<Map<String, Object>> dto = BaseDTO.buildSuccess(map);
            map.put("status", status);
            map.put("max", AccountManager.MAXCERT);
            map.put("header", status == 4 ? noticeManager.getUserHeader(uid) : null);
            if (userReal != null) {
                map.put("count", userReal.getCommitCount());
                map.put("card", userReal.getIdNumber());
                map.put("name", userReal.getRealName());
                map.put("quick", AccountManager.isQuck(userReal.getCommitCount()));
                dto.setMessage(status == 3 ? userReal.getRemark() : null);
            } else {
                map.put("count", 0);
                map.put("card", 0);
                map.put("name", "");
                map.put("quick", true);
            }
            return dto.convert();
        } catch (Exception e) {
            log.error("获取用户{}的实名认证状态出错", uid, e);
            return BaseDTO.buildError("获取实名认证状态出错").convert();
        }
    }

    /**
     * 发送验证码
     *
     * @param uid 用户UID
     * @return 返回发送结果（具体结果参照短信服务）
     */
    @RequestMapping(value = "sendSms", method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> sendSms(@RequestParam("userId") long uid) {
        log.info("用户{}申请发送验证码", uid);
        try {
            return BaseDTO.buildSuccess(accountManager.sendCashApplySms(uid)).convert();
        } catch (Exception e) {
            log.error("用户{}申请发送验证码", uid, e);
            return BaseDTO.buildError("发送验证码失败").convert();
        }
    }


    /**
     * 获取当前用户的绑定手机号
     *
     * @param uid 用户UID
     * @return 当前用户绑定手机号
     */
    @RequestMapping(value = "getPhone", method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getPhone(@RequestParam("userId") long uid) {
        log.debug("用户{}获取手机号", uid);
        try {
            return BaseDTO.buildSuccess(accountManager.getPhone(uid)).convert();
        } catch (Exception e) {
            log.error("用户{}获取手机号失败", uid, e);
            return BaseDTO.buildError("获取手机号失败").convert();
        }
    }

    /**
     * 获取当前用户的真实姓名
     *
     * @param uid 当前用户的uid
     * @return 当前用户的真实姓名
     */
    @RequestMapping(value = "getRealName", method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getRealName(@RequestParam("userId") long uid) {
        log.debug("用户{}获取实名认证姓名", uid);
        try {
            return BaseDTO.buildSuccess(accountManager.getRealName(uid, true)).convert();
        } catch (Exception e) {
            log.error("用户{}获取实名认证姓名失败", uid, e);
            return BaseDTO.buildError("获取实名认证姓名失败").convert();
        }
    }
}
